package com.woniuxy.microcontentserver.config;

import com.woniuxy.microcontentserver.handler.StringToDateConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMvcConfigure implements WebMvcConfigurer {
    /**
     * 跨域支持
     * @param registry r
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //允许所有的请求
                .allowedOrigins("*")
                //允许做身份认证
                .allowCredentials(true).
                //允许支持的跨域方法有哪些
                allowedMethods("GET","POST","PUT","DELETE");
    }

    /**
     * 配置类型转换器
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(stringToDateConverter());

    }

    /**
     * @Bean 用于将组件的实例，装配到Spring容器中
     * 方法的方法名：stringToDateConverter 作为组件在容器的ID
     * 默认：作用域为：单例
     * @return
     */
    @Bean
    public StringToDateConverter stringToDateConverter(){
        return new StringToDateConverter();
    }
}
